// pages/classify/classify.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      show:true,
      activeId:1,
      leftNav:null,
      right_nav:null
  },
  changeTab(event,sonId=null){
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    if (!sonId){
      var id = event.currentTarget.dataset.id;
    }else{
      var id = event;
    }
    
    this.setData({
      activeId:id
    })
    if (!sonId){
      this.getCateSon(this.data.activeId)
    }else{
      console.log("SON",sonId)
      this.getCateSon(this.data.activeId,sonId)
    }
   
  },
  toggle(){
    this.setData({
      show:!this.data.show
    })
  },
  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //获取子分类
  getCateSon(id,sonId){
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Index/cateSon', "POST", { key, parent_id:id }, function (res){
      wx.hideLoading();
      if(res.data.code==200){
        that.setData({
          right_nav: res.data.data,
          toView:'item'+sonId
        })
      }else{
        wx.showToast({
          title: res.data.msg,
          mask:true,
          icon:'none'
        })
      }
      
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  toDetail(event){
    app.globalData.cateId = this.data.activeId;
    
    wx.navigateTo({
      url: '../detail/detail?id='+event.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app)
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    var key = app.getKey();
    var that = this;
    if(app.globalData.cateId){
      that.setData({
        activeId: app.globalData.cateId
      })
    }
    // if(this.data.leftNav){
    //   wx.hideLoading();
    //   return false;
    // }
    app.ajax('/Home/Index/cateParent', "POST", { key, }, function (res) {
      if (res.data.code == 200) {
        that.setData({
          leftNav: res.data.data,
          
        })
        if (app.globalData.cateId) {
          that.setData({
            activeId: app.globalData.cateId
          })
        }else{
          that.setData({
            activeId: res.data.data[0].id
          })
        }
        that.getCateSon(that.data.activeId)
      } else {
        wx.showToast({
          title: res.data.msg,
          mask: true,
          icon: 'none'
        })
      }
    })
  },
  search(e){
   
    var value = e.detail.value;
    var key = app.getKey();
    var that = this;
    app.ajax('/Home/Index/searchDetail', "POST", { key,title:value }, function (res) {
      if(res.data.code==200){
          if(res.data.data.length==0){
            return false;
          }else{
            that.changeTab(res.data.data[0].parent_id, res.data.data[0].son_id);
          }
      }
      // that.setData({
      //   imgUrls: res.data.data,
      //   successNum: that.data.successNum + 1
      // })
      // that.isLoaded()
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})